package com.hospitalmanagementsystem.config;

import com.hospitalmanagementsystem.entity.*;
import com.hospitalmanagementsystem.enums.AppointmentStatus;
import com.hospitalmanagementsystem.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private DoctorRepository doctorRepository;
    
    @Autowired
    private DepartmentRepository departmentRepository;
    
    @Autowired
    private AppointmentRepository appointmentRepository;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否已有预约数据
        if (appointmentRepository.count() == 0) {
            // 获取已有的用户、医生和科室数据
            List<User> patients = userRepository.findAll();
            List<Doctor> doctors = doctorRepository.findAll();
            List<Department> departments = departmentRepository.findAll();
            
            if (!patients.isEmpty() && !doctors.isEmpty() && !departments.isEmpty()) {
                // 创建测试预约数据
                User patient = patients.stream()
                    .filter(u -> u.getRoles().contains("ROLE_USER"))
                    .findFirst()
                    .orElse(patients.get(0));
                
                Doctor doctor = doctors.get(0);
                Department department = departments.get(0);
                
                // 创建3个不同状态的预约
                Appointment appointment1 = new Appointment();
                appointment1.setPatient(patient);
                appointment1.setDoctor(doctor);
                appointment1.setDepartment(department);
                appointment1.setAppointmentDate(LocalDate.now().plusDays(1));
                appointment1.setAppointmentTime(LocalTime.of(9, 0));
                appointment1.setReason("常规体检");
                appointment1.setStatus(AppointmentStatus.PENDING);
                appointment1.setNotes("首次体检");
                appointmentRepository.save(appointment1);
                
                Appointment appointment2 = new Appointment();
                appointment2.setPatient(patient);
                appointment2.setDoctor(doctor);
                appointment2.setDepartment(department);
                appointment2.setAppointmentDate(LocalDate.now().plusDays(2));
                appointment2.setAppointmentTime(LocalTime.of(14, 30));
                appointment2.setReason("复诊");
                appointment2.setStatus(AppointmentStatus.CONFIRMED);
                appointment2.setNotes("需要带上之前的检查结果");
                appointmentRepository.save(appointment2);
                
                Appointment appointment3 = new Appointment();
                appointment3.setPatient(patient);
                appointment3.setDoctor(doctor);
                appointment3.setDepartment(department);
                appointment3.setAppointmentDate(LocalDate.now().minusDays(1));
                appointment3.setAppointmentTime(LocalTime.of(10, 15));
                appointment3.setReason("头痛");
                appointment3.setStatus(AppointmentStatus.COMPLETED);
                appointment3.setNotes("已完成诊断和治疗");
                appointmentRepository.save(appointment3);
                
                System.out.println("创建了3条测试预约数据");
            }
        }
    }
} 